package cn.exrick.xboot.modules.bicmktbiddinginfo.serviceimpl;

import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.modules.biddataproject.entity.SelectData;
import cn.exrick.xboot.modules.bicmktbiddinginfo.entity.*;
import cn.exrick.xboot.modules.bicmktbiddinginfo.mapper.BicMktBiddingInfoMapper;
import cn.exrick.xboot.modules.bicmktbiddinginfo.service.IBicMktBiddingInfoService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 招投标信息接口实现
 *
 * @author sdtf
 */
@Slf4j
@Service
@Transactional
public class IBicMktBiddingInfoServiceImpl extends ServiceImpl<BicMktBiddingInfoMapper, BicMktBiddingInfo> implements IBicMktBiddingInfoService {

    @Autowired
    private BicMktBiddingInfoMapper bicMktBiddingInfoMapper;

    /**
     * 分年投中标数据
     */
    @Override
    public List<BidAndBidBack> getSanNianbidding(String i) {
        //投标数量和投标金额
        List<BidAndBidBack> list = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyByNian(i);
        //省内投标数量和投标金额
        List<BidAndBidBack> list1 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyInProvinceByNian(i);
        //省外投标数量和投标金额
        List<BidAndBidBack> list2 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyOutProvinceByNian(i);
        //省内中标数量和中标金额
        List<BidAndBidBack> list4 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyInProvince1ByNian(i);
        //省外中标数量和中标金额
        List<BidAndBidBack> list5 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyOutProvince1ByNian(i);
        //循环填充数据
        if (list != null) {
            for (BidAndBidBack bidAndBidBack : list) {
                //填充数据省内投标数量和投标金额
                for (BidAndBidBack andBidBack : list1) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //省内投标数量和投标金额
                        bidAndBidBack.setBidNumberInProvince(andBidBack.getBidNumberInProvince());
                        bidAndBidBack.setBidMoneyInProvince(andBidBack.getBidMoneyInProvince());
                    }
                }
                //填充数据省外投标数量和投标金额
                for (BidAndBidBack andBidBack : list2) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //省外投标数量和投标金额
                        bidAndBidBack.setBidNumberOutProvince(andBidBack.getBidNumberOutProvince());
                        bidAndBidBack.setBidMoneyOutProvince(andBidBack.getBidMoneyOutProvince());
                    }
                }
                //填充数据省内中标数量和中标金额
                for (BidAndBidBack andBidBack : list4) {
                    if (andBidBack.getDeptName().equals
                            (bidAndBidBack.getDeptName())) {
                        //省内中标数量和中标金额
                        bidAndBidBack.setBidBackNumberInProvince(andBidBack.getBidBackNumberInProvince());
                        bidAndBidBack.setBidBackMoneyInProvince(andBidBack.getBidBackMoneyInProvince());
                    }
                }
                //填充数据省外中标数量和中标金额
                for (BidAndBidBack andBidBack : list5) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //省外中标数量和中标金额
                        bidAndBidBack.setBidBackNumberOutProvince(andBidBack.getBidBackNumberOutProvince());
                        bidAndBidBack.setBidBackMoneyOutProvince(andBidBack.getBidBackMoneyOutProvince());
                    }
                }
            }
        }
        System.out.println(list.size());
        return list;
    }


    /**
     * 中投标数量金额本年
     */
    @Override
    public Map<String, Object> BidAndBidBackNumberAndMoneyByYear() {
        //获取有数据的所有省份
        String[] province = bicMktBiddingInfoMapper.getProvince();
        ArrayList<String> objects = new ArrayList<>();
        for (String s : province) {
            if (!s.equals("")) {
                objects.add(s);
            }
        }
        Object[] objects1 = objects.toArray();
        //获取有数据的所有单位
        String[] deptName = bicMktBiddingInfoMapper.getDeptName();
        ArrayList<String> dep = new ArrayList<>();
        for (String s : deptName) {
            dep.add(s.substring(0, 2));
        }
        List bidAndBidBacksa = new ArrayList<>();
        for (String s1 : deptName) {
            List<BidAndBidBack> bidAndBidBacks = new ArrayList<>();
            for (String s2 : province) {
                if (!s2.equals("")) {
                    BidAndBidBack dataByDeptNameAndProvince = bicMktBiddingInfoMapper.getDataByDeptNameAndProvince(s1, s2);
                    if (dataByDeptNameAndProvince != null) {
                        bidAndBidBacks.add(dataByDeptNameAndProvince);
                    } else {
                        bidAndBidBacks.add(new BidAndBidBack());
                    }
                }
            }
            bidAndBidBacksa.add(bidAndBidBacks);
            System.out.println(bidAndBidBacks.size());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("buMendata", bidAndBidBacksa);
        map.put("shegFenData", objects1);
        map.put("bumen", dep);
        return map;
    }

    /**
     * 中投标数量金额本月
     */
    @Override
    public Map<String, Object> BidAndBidBackNumberAndMoneyByM() {
        //获取有数据的所有省份
        String[] province = bicMktBiddingInfoMapper.getProvinceM();
        ArrayList<String> objects = new ArrayList<>();
        for (String s : province) {
            if (!s.equals("")) {
                objects.add(s);
            }
        }
        Object[] objects1 = objects.toArray();
        //获取有数据的所有单位
        String[] deptName = bicMktBiddingInfoMapper.getDeptNameM();
        ArrayList<String> dep = new ArrayList<>();
        for (String s : deptName) {
            dep.add(s.substring(0, 2));
        }
        List bidAndBidBacksa = new ArrayList<>();
        for (String s1 : deptName) {
            List<BidAndBidBack> bidAndBidBacks = new ArrayList<>();
            for (String s2 : province) {
                if (!s2.equals("")) {
                    BidAndBidBack dataByDeptNameAndProvince = bicMktBiddingInfoMapper.getDataByDeptNameAndProvinceM(s1, s2);
                    if (dataByDeptNameAndProvince != null) {
                        bidAndBidBacks.add(dataByDeptNameAndProvince);
                    } else {
                        bidAndBidBacks.add(new BidAndBidBack());
                    }
                }
            }
            bidAndBidBacksa.add(bidAndBidBacks);
            System.out.println(bidAndBidBacks.size());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("buMendata", bidAndBidBacksa);
        map.put("shegFenData", objects1);
        map.put("bumen", dep);
        return map;
    }

    /**
     * 省外中投标数量金额本年
     */
    @Override
    public List<BidAndBidBack> BidAndBidBackNumberAndMoneyByYear1() {
        //总投标数量和投标金额
        List<BidAndBidBack> list = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoney();
        //中标数量和中标金额
        List<BidAndBidBack> list3 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoney1();
        //省外中标
        List<BidAndBidBack> bidAndBidBacksB = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyOutProvince1AndArea();
        //省外投标
        List<BidAndBidBack> bidAndBidBacksA = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyOutProvince1AndArea();
        for (BidAndBidBack bidAndBidBack : list) {
            //填充数据省内投标数量和投标金额
            for (BidAndBidBack andBidBack : bidAndBidBacksA) {
                if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                    //省内投标数量和投标金额
                    bidAndBidBack.setBidNumberInProvince(andBidBack.getBidNumberInProvince());
                    bidAndBidBack.setBidMoneyInProvince(andBidBack.getBidMoneyInProvince());
                }
            }
            //填充数据省内中标数量和中标金额
            for (BidAndBidBack andBidBack : bidAndBidBacksB) {
                if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                    //省内中标数量和中标金额
                    bidAndBidBack.setBidBackNumberInProvince(andBidBack.getBidBackNumberInProvince());
                    bidAndBidBack.setBidBackMoneyInProvince(andBidBack.getBidBackMoneyInProvince());
                }
            }
        }
        return null;
    }

    /**
     * 最新中标项目
     */
    @Override
    public List<SelectData> selectNewBid() {
        List<SelectData> selectData = bicMktBiddingInfoMapper.selectNewBid();
        int order = 1;
        if (selectData != null) {
            for (SelectData selectDatum : selectData) {
//              时间格式转化
                String createDate = selectDatum.getCreateDate();
                String dateSubstring = createDate.substring(0, 8);
                selectDatum.setCreateDate(dateSubstring);
            }
        }
        return selectData;
    }

    /**
     * 在投重大项目
     */
    @Override
    public List<SelectData> selectisBidding() {
        List<SelectData> selectData = bicMktBiddingInfoMapper.selectisBidding();
        int order = 1;
        if (selectData != null) {
            for (SelectData selectDatum : selectData) {
                String createDate = selectDatum.getCreateDate();
                String dateSubstring = createDate.substring(0, 8);
                selectDatum.setCreateDate(dateSubstring);
                //设置排名
                selectDatum.setOrder(order);
                order++;
            }
        }
        return selectData;
    }

    /**
     * 本年投中标数
     */
    @Override
    public SelectData selectBidNumber() {
        SelectData selectData = new SelectData();
        //查询投标数
        SelectData selectData0 = bicMktBiddingInfoMapper.selectBidNumber0();
        //查询投标金额
        if (selectData0.getBidsNumber0Amount() == null) {
            selectData.setBidsNumber0Amount(0L);
        } else {
            Double bidsNumber0Amount = selectData0.getBidsNumber0Amount() / 10000.0;
            long longValue0 = Math.round(bidsNumber0Amount);
            selectData.setBidsNumber0Amount(longValue0);
        }
        //查询中标数
        SelectData selectData1 = bicMktBiddingInfoMapper.selectBidNumber1();
        //查询中标金额
        if (selectData1.getBidsNumber1Amount() == null) {
            selectData.setBidsNumber1Amount(0L);
        } else {
            Double bidsNumber1Amount = selectData1.getBidsNumber1Amount() / 10000.0;
            long longValue1 = Math.round(bidsNumber1Amount);
            selectData.setBidsNumber1Amount(longValue1);
        }
        //封装返回对象
        selectData.setBidsNumber0(selectData0.getBidsNumber0());
        selectData.setBidsNumber1(selectData1.getBidsNumber1());
        return selectData;
    }

    /**
     * 本月投中标数
     */
    @Override
    public SelectData BidNumberByMonth() {
        SelectData selectData = new SelectData();
        //查询投标数
        SelectData selectData0 = bicMktBiddingInfoMapper.selectBidNumber0ByMonth();
        //查询投标金额
        if (selectData0.getBidsNumber0Amount() == null) {
            selectData.setBidsNumber0Amount(0L);
        } else {
            Double bidsNumber0Amount = selectData0.getBidsNumber0Amount() / 10000.0;
            long longValue0 = Math.round(bidsNumber0Amount);
            selectData.setBidsNumber0Amount(longValue0);
        }
        //查询中标数
        SelectData selectData1 = bicMktBiddingInfoMapper.selectBidNumber1ByMonth();
        //查询中标金额
        if (selectData1.getBidsNumber1Amount() == null) {
            selectData.setBidsNumber1Amount(0L);
        } else {
            Double bidsNumber1Amount = selectData1.getBidsNumber1Amount() / 10000.0;
            long longValue1 = Math.round(bidsNumber1Amount);
            selectData.setBidsNumber1Amount(longValue1);
        }
        //封装返回对象
        selectData.setBidsNumber0(selectData0.getBidsNumber0());
        selectData.setBidsNumber1(selectData1.getBidsNumber1());
        return selectData;
    }

    /**
     * 正在投标
     */
    @Override
    public SelectData Bidding() {
        SelectData selectData = bicMktBiddingInfoMapper.selectBidding();
        return selectData;
    }

    /**
     * 本年中标排名
     */
    @Override
    public List<SelectData> BidInfo() {
//        后端查询数据并且返回
        List<SelectData> selectData = bicMktBiddingInfoMapper.selectBidInfo();
        return selectData;
    }

    /**
     * 本月中标排名
     */
    @Override
    public List<SelectData> BidInfoByMonth() {
        //        后端查询数据并且返回
        List<SelectData> selectData = bicMktBiddingInfoMapper.BidInfoByMonth();
        return selectData;
    }

    /**
     * 二级菜单：最新中标项目详情本年
     */
    @Override
    public List<BidInfo> NewBidInformation(PageVo page) {
        int pageSize = page.getPageSize();
        int pageNumber = (page.getPageNumber() - 1) * pageSize;
        List<BidInfo> list = bicMktBiddingInfoMapper.NewBidInformation(pageNumber, pageSize);
        List<BidInfo> bidInfos = bicMktBiddingInfoMapper.NewBidInformation1();
        if (list != null) {
            for (BidInfo bidInfo : list) {
                //时间格式进行优化
                String createDate = bidInfo.getBidBackTime();
                String dateSubstring = createDate.substring(0, 8);
                bidInfo.setBidBackTime(dateSubstring);
                int size1 = bidInfos.size();
                if (size1 != 0) {
                    Long total = (long) size1;
                    bidInfo.setTotal(total);
                } else {
                    bidInfo.setTotal(0L);
                }
            }
        }
        return list;
    }

    /**
     * 二级菜单：最新中标项目详情本月
     */
    @Override
    public List<BidInfo> NewBidInformationByMonth(PageVo page) {
        int pageSize = page.getPageSize();
        int pageNumber = (page.getPageNumber() - 1) * pageSize;
        List<BidInfo> list1 = bicMktBiddingInfoMapper.NewBidInformationByMonth();
        List<BidInfo> list = bicMktBiddingInfoMapper.NewBidInformationByMonth1(pageNumber, pageSize);
        if (list != null) {
            for (BidInfo bidInfo : list) {
                //时间格式进行优化
                String createDate = bidInfo.getBidBackTime();
                String dateSubstring = createDate.substring(0, 8);
                bidInfo.setBidBackTime(dateSubstring);
                int size1 = list1.size();
                if (size1 != 0) {
                    Long total = (long) size1;
                    bidInfo.setTotal(total);
                } else {
                    bidInfo.setTotal(0L);
                }
            }
        }
        return list;
    }

    /**
     * 二级菜单：在投重大项目详情本年
     */
    @Override
    public List<BidInfo> isBiddingInformation(PageVo page) {
        int pageSize = page.getPageSize();
        int pageNumber = (page.getPageNumber() - 1) * pageSize;
        List<BidInfo> list1 = bicMktBiddingInfoMapper.isBiddingInformation();
        List<BidInfo> list = bicMktBiddingInfoMapper.isBiddingInformation1(pageNumber, pageSize);
        if (list != null) {
            for (BidInfo bidInfo : list) {
                //时间格式进行优化
                String createDate = bidInfo.getBidBackTime();
                String dateSubstring = createDate.substring(0, 8);
                bidInfo.setBidBackTime(dateSubstring);
                int size1 = list1.size();
                if (size1 != 0) {
                    Long total = (long) size1;
                    bidInfo.setTotal(total);
                } else {
                    bidInfo.setTotal(0L);
                }
            }
        }
        return list;
    }

    /**
     * 二级菜单：在投重大项目详情本月
     */
    @Override
    public List<BidInfo> isBiddingInformationByMonth() {
        List<BidInfo> list = bicMktBiddingInfoMapper.isBiddingInformationByMonth();
        if (list != null) {
            for (BidInfo bidInfo : list) {
                //时间格式进行优化
                String createDate = bidInfo.getBidBackTime();
                String dateSubstring = createDate.substring(0, 8);
                bidInfo.setBidBackTime(dateSubstring);
            }
        }
        return list;
    }


    /**
     * 废弃方法
     */
    @Override
    public List<BidInfo> isBiddingInformationByDate(SelectDate selectDate) {
        String startDate = selectDate.getStartDate().toString();
        String endDate = selectDate.getEndDate().toString();
        List<BidInfo> list = bicMktBiddingInfoMapper.isBiddingInformationByDate(startDate, endDate);
        if (list != null) {
            for (BidInfo bidInfo : list) {
                //时间格式进行优化
                String createDate = bidInfo.getBidBackTime();
                String dateSubstring = createDate.substring(0, 8);
                bidInfo.setBidBackTime(dateSubstring);
            }
        }
        return list;
    }


    /**
     * 二级菜单：投标中标数量金额详情--本年
     */
    @Override
    public List<BidAndBidBack> BidAndBidBackNumberAndMoney(BidAndBidBack bidtype) {
        //查询出所有单位名称
        List<BidAndBidBack> list6 = bicMktBiddingInfoMapper.selectdept();
        //投标数量和投标金额
        List<BidAndBidBack> list = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoney();
        //省内投标数量和投标金额
        List<BidAndBidBack> list1 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyInProvince();
        //省外投标数量和投标金额
        List<BidAndBidBack> list2 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyOutProvince();
        //中标数量和中标金额
        List<BidAndBidBack> list3 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoney1();
        //省内中标数量和中标金额
        List<BidAndBidBack> list4 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyInProvince1();
        //省外中标数量和中标金额
        List<BidAndBidBack> list5 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyOutProvince1();
        //循环填充数据
        if (list6 != null) {
            for (BidAndBidBack bidAndBidBack : list6) {
//                填充投标数量和投标金额
                for (BidAndBidBack andBidBack : list) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        bidAndBidBack.setBidNumber(andBidBack.getBidNumber());
                        bidAndBidBack.setBidMoney(andBidBack.getBidMoney());
                    }
                }
//                填充投标数量和投标金额
                for (BidAndBidBack andBidBack : list1) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //省内投标数量和投标金额
                        bidAndBidBack.setBidNumberInProvince(andBidBack.getBidNumberInProvince());
                        bidAndBidBack.setBidMoneyInProvince(andBidBack.getBidMoneyInProvince());
                    }
                }
                //填充数据省外投标数量和投标金额
                for (BidAndBidBack andBidBack : list2) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //省外投标数量和投标金额
                        bidAndBidBack.setBidNumberOutProvince(andBidBack.getBidNumberOutProvince());
                        bidAndBidBack.setBidMoneyOutProvince(andBidBack.getBidMoneyOutProvince());
                    }
                }
                //填充数据中标数量和中标金额
                for (BidAndBidBack andBidBack : list3) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //中标数量和中标金额
                        bidAndBidBack.setBidBackNumber(andBidBack.getBidBackNumber());
                        bidAndBidBack.setBidBackMoney(andBidBack.getBidBackMoney());
                    }
                }
                //填充数据省内中标数量和中标金额
                for (BidAndBidBack andBidBack : list4) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //省内中标数量和中标金额
                        bidAndBidBack.setBidBackNumberInProvince(andBidBack.getBidBackNumberInProvince());
                        bidAndBidBack.setBidBackMoneyInProvince(andBidBack.getBidBackMoneyInProvince());
                    }
                }
                //填充数据省外中标数量和中标金额
                for (BidAndBidBack andBidBack : list5) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //省外中标数量和中标金额
                        bidAndBidBack.setBidBackNumberOutProvince(andBidBack.getBidBackNumberOutProvince());
                        bidAndBidBack.setBidBackMoneyOutProvince(andBidBack.getBidBackMoneyOutProvince());
                    }
                }
            }
        }
        //循环，对部门进行优化展示
        for (BidAndBidBack bidAndBidBack : list6) {
            //对null值进行优化
            //年度总数量
            if (bidAndBidBack.getBidNumber() == null) {
                bidAndBidBack.setBidNumber(0);
            }
            //年度总金额
            if (bidAndBidBack.getBidMoney() == null) {
                bidAndBidBack.setBidMoney(0L);
            }
            //数量省内
            if (bidAndBidBack.getBidNumberInProvince() == null) {
                bidAndBidBack.setBidNumberInProvince(0);
            }
            //数量省外
            if (bidAndBidBack.getBidNumberOutProvince() == null) {
                bidAndBidBack.setBidNumberOutProvince(0);
            }
            //金额省内
            if (bidAndBidBack.getBidMoneyInProvince() == null) {
                bidAndBidBack.setBidMoneyInProvince(0L);
            }
            //金额省外
            if (bidAndBidBack.getBidMoneyOutProvince() == null) {
                bidAndBidBack.setBidMoneyOutProvince(0L);
            }
            //中标数量
            if (bidAndBidBack.getBidBackNumber() == null) {
                bidAndBidBack.setBidBackNumber(0);
            }
            //中标金额
            if (bidAndBidBack.getBidBackMoney() == null) {
                bidAndBidBack.setBidBackMoney(0L);
            }
            //中标数量省内
            if (bidAndBidBack.getBidBackNumberInProvince() == null) {
                bidAndBidBack.setBidBackNumberInProvince(0);
            }
            //中标金额省内
            if (bidAndBidBack.getBidBackMoneyInProvince() == null) {
                bidAndBidBack.setBidBackMoneyInProvince(0L);
            }
            //中标数量省外
            if (bidAndBidBack.getBidBackNumberOutProvince() == null) {
                bidAndBidBack.setBidBackNumberOutProvince(0);
            }
            //中标金额省外
            if (bidAndBidBack.getBidBackMoneyOutProvince() == null) {
                bidAndBidBack.setBidBackMoneyOutProvince(0L);
            }
        }
        //对集合中对数据进行排序处理,根据传进来的参数进行判断是投标还是中标
        if (bidtype.getType() != null) {
            if (bidtype.getType() == 1) {
                List<BidAndBidBack> collect = list6.stream()
                        .sorted(Comparator.comparing(BidAndBidBack::getBidMoney).reversed())
                        .collect(Collectors.toList());
                return collect;
            } else {
                List<BidAndBidBack> collect = list6.stream()
                        .sorted(Comparator.comparing(BidAndBidBack::getBidBackMoney).reversed())
                        .collect(Collectors.toList());
                return collect;
            }
        }
        return list6;
    }

    /**
     * 二级菜单：投标中标数量金额详情--本月
     */
    @Override
    public List<BidAndBidBack> BidAndBidBackNumberAndMoneyByMonth(BidAndBidBack bidtype) {
        //查询出所有单位名称
        List<BidAndBidBack> list6 = bicMktBiddingInfoMapper.selectdept();
        //投标数量和投标金额
        List<BidAndBidBack> list = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyByMonth();
        //省内投标数量和投标金额
        List<BidAndBidBack> list1 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyInProvinceByMonth();
        //省外投标数量和投标金额
        List<BidAndBidBack> list2 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyOutProvinceByMonth();
        //中标数量和中标金额
        List<BidAndBidBack> list3 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoney1ByMonth();
        //省内中标数量和中标金额
        List<BidAndBidBack> list4 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyInProvince1ByMonth();
        //省外中标数量和中标金额
        List<BidAndBidBack> list5 = bicMktBiddingInfoMapper.BidAndBidBackNumberAndMoneyOutProvince1ByMonth();
        //循环填充数据
        if (list6 != null) {
            for (BidAndBidBack bidAndBidBack : list6) {
//                填充投标数量和投标金额
                for (BidAndBidBack andBidBack : list) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        bidAndBidBack.setBidNumber(andBidBack.getBidNumber());
                        bidAndBidBack.setBidMoney(andBidBack.getBidMoney());
                    }
                }
//                填充投标数量和投标金额
                for (BidAndBidBack andBidBack : list1) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //省内投标数量和投标金额
                        bidAndBidBack.setBidNumberInProvince(andBidBack.getBidNumberInProvince());
                        bidAndBidBack.setBidMoneyInProvince(andBidBack.getBidMoneyInProvince());
                    }
                }
                //填充数据省外投标数量和投标金额
                for (BidAndBidBack andBidBack : list2) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //省外投标数量和投标金额
                        bidAndBidBack.setBidNumberOutProvince(andBidBack.getBidNumberOutProvince());
                        bidAndBidBack.setBidMoneyOutProvince(andBidBack.getBidMoneyOutProvince());
                    }
                }
                //填充数据中标数量和中标金额
                for (BidAndBidBack andBidBack : list3) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //中标数量和中标金额
                        bidAndBidBack.setBidBackNumber(andBidBack.getBidBackNumber());
                        bidAndBidBack.setBidBackMoney(andBidBack.getBidBackMoney());
                    }
                }
                //填充数据省内中标数量和中标金额
                for (BidAndBidBack andBidBack : list4) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //省内中标数量和中标金额
                        bidAndBidBack.setBidBackNumberInProvince(andBidBack.getBidBackNumberInProvince());
                        bidAndBidBack.setBidBackMoneyInProvince(andBidBack.getBidBackMoneyInProvince());
                    }
                }
                //填充数据省外中标数量和中标金额
                for (BidAndBidBack andBidBack : list5) {
                    if (andBidBack.getDeptName().equals(bidAndBidBack.getDeptName())) {
                        //省外中标数量和中标金额
                        bidAndBidBack.setBidBackNumberOutProvince(andBidBack.getBidBackNumberOutProvince());
                        bidAndBidBack.setBidBackMoneyOutProvince(andBidBack.getBidBackMoneyOutProvince());
                    }
                }
            }
        }
        //循环，对部门进行优化展示
        for (BidAndBidBack bidAndBidBack : list6) {
            //年度总数量
            if (bidAndBidBack.getBidNumber() == null) {
                bidAndBidBack.setBidNumber(0);
            }
            //年度总金额
            if (bidAndBidBack.getBidMoney() == null) {
                bidAndBidBack.setBidMoney(0L);
            }
            //数量省内
            if (bidAndBidBack.getBidNumberInProvince() == null) {
                bidAndBidBack.setBidNumberInProvince(0);
            }
            //数量省外
            if (bidAndBidBack.getBidNumberOutProvince() == null) {
                bidAndBidBack.setBidNumberOutProvince(0);
            }
            //金额省内
            if (bidAndBidBack.getBidMoneyInProvince() == null) {
                bidAndBidBack.setBidMoneyInProvince(0L);
            }
            //金额省外
            if (bidAndBidBack.getBidMoneyOutProvince() == null) {
                bidAndBidBack.setBidMoneyOutProvince(0L);
            }
            //中标数量
            if (bidAndBidBack.getBidBackNumber() == null) {
                bidAndBidBack.setBidBackNumber(0);
            }
            //中标金额
            if (bidAndBidBack.getBidBackMoney() == null) {
                bidAndBidBack.setBidBackMoney(0L);
            }
            //中标数量省内
            if (bidAndBidBack.getBidBackNumberInProvince() == null) {
                bidAndBidBack.setBidBackNumberInProvince(0);
            }
            //中标金额省内
            if (bidAndBidBack.getBidBackMoneyInProvince() == null) {
                bidAndBidBack.setBidBackMoneyInProvince(0L);
            }
            //中标数量省外
            if (bidAndBidBack.getBidBackNumberOutProvince() == null) {
                bidAndBidBack.setBidBackNumberOutProvince(0);
            }
            //中标金额省外
            if (bidAndBidBack.getBidBackMoneyOutProvince() == null) {
                bidAndBidBack.setBidBackMoneyOutProvince(0L);
            }
        }
        //对集合中对数据进行排序处理,根据传进来的参数进行判断是投标还是中标
        if (bidtype.getType() != null) {
            if (bidtype.getType() == 1) {
                List<BidAndBidBack> collect = list6.stream()
                        .sorted(Comparator.comparing(BidAndBidBack::getBidMoney).reversed())
                        .collect(Collectors.toList());
                return collect;
            } else {
                List<BidAndBidBack> collect = list6.stream()
                        .sorted(Comparator.comparing(BidAndBidBack::getBidBackMoney).reversed())
                        .collect(Collectors.toList());
                return collect;
            }
        }
        return list6;
    }


    /**
     * 二级菜单：正在投标数量金额详情本年
     */
    @Override
    public List<IsBiddingInfo> isBiddingNumAndMoney() {
        List<IsBiddingInfo> list3 = bicMktBiddingInfoMapper.getdept();
//        z正在投标数量和金额
        List<IsBiddingInfo> list = bicMktBiddingInfoMapper.isBiddingNumAndMoney();
//        省内正在投标数量和金额
        List<IsBiddingInfo> list1 = bicMktBiddingInfoMapper.isBiddingNumAndMoneyInProVince();
//        省外正在投标数量和金额
        List<IsBiddingInfo> list2 = bicMktBiddingInfoMapper.isBiddingNumAndMoneyOutProVince();
        if (list3 != null) {
            for (IsBiddingInfo isBiddingInfo : list3) {
//                填充正在投标数量和金额
                for (IsBiddingInfo biddingInfo : list) {
                    if (biddingInfo.getDeptName().equals(isBiddingInfo.getDeptName())){
                        isBiddingInfo.setIsBiddingNumber(biddingInfo.getIsBiddingNumber());
                        isBiddingInfo.setIsBiddingMoney(biddingInfo.getIsBiddingMoney());
                    }
                }
                //填充省内正在投标数和中标数量
                for (IsBiddingInfo biddingInfo : list1) {
                    if (biddingInfo.getDeptName().equals(isBiddingInfo.getDeptName())) {
                        isBiddingInfo.setIsBiddingNumberInProvince(biddingInfo.getIsBiddingNumberInProvince());
                        isBiddingInfo.setIsBiddingMoneyInProvince(biddingInfo.getIsBiddingMoneyInProvince());
                    }
                }
                //填充省外正在投标数和中标数量
                for (IsBiddingInfo biddingInfo : list2) {
                    if (biddingInfo.getDeptName().equals(isBiddingInfo.getDeptName())) {
                        isBiddingInfo.setIsBiddingNumberOutProvince(biddingInfo.getIsBiddingNumberOutProvince());
                        isBiddingInfo.setIsBiddingMoneyOutProvince(biddingInfo.getIsBiddingMoneyOutProvince());
                    }
                }
            }
        }
        for (IsBiddingInfo isBiddingInfo : list3) {
            //封装数据
            if (isBiddingInfo.getIsBiddingNumber() == null) {
                isBiddingInfo.setIsBiddingNumber(0);
            }
            if (isBiddingInfo.getIsBiddingMoney() == null) {
                isBiddingInfo.setIsBiddingMoney(0L);
            }
            if (isBiddingInfo.getIsBiddingNumberInProvince() == null) {
                isBiddingInfo.setIsBiddingNumberInProvince(0);
            }
            if (isBiddingInfo.getIsBiddingMoneyInProvince() == null) {
                isBiddingInfo.setIsBiddingMoneyInProvince(0L);
            }
            if (isBiddingInfo.getIsBiddingNumberOutProvince() == null) {
                isBiddingInfo.setIsBiddingNumberOutProvince(0);
            }
            if (isBiddingInfo.getIsBiddingMoneyOutProvince() == null) {
                isBiddingInfo.setIsBiddingMoneyOutProvince(0L);
            }
        }
        List<IsBiddingInfo> collect = list3.stream().sorted(Comparator.comparing(IsBiddingInfo::getIsBiddingMoney).reversed()).collect(Collectors.toList());
        return collect;
    }

    /**
     * 二级菜单：正在投标数量金额详情本月
     */
    @Override
    public List<IsBiddingInfo> isBiddingNumAndMoneyByMonth() {
        List<IsBiddingInfo> list3 = bicMktBiddingInfoMapper.getdept();
        List<IsBiddingInfo> list = bicMktBiddingInfoMapper.isBiddingNumAndMoneyByMonth();
        List<IsBiddingInfo> list1 = bicMktBiddingInfoMapper.isBiddingNumAndMoneyInProVinceByMonth();
        List<IsBiddingInfo> list2 = bicMktBiddingInfoMapper.isBiddingNumAndMoneyOutProVinceByMonth();
        if (list3 != null) {
            for (IsBiddingInfo isBiddingInfo : list3) {
//                填充正在投标数量和金额
                for (IsBiddingInfo biddingInfo : list) {
                    if (biddingInfo.getDeptName().equals(isBiddingInfo.getDeptName())){
                        isBiddingInfo.setIsBiddingNumber(biddingInfo.getIsBiddingNumber());
                        isBiddingInfo.setIsBiddingMoney(biddingInfo.getIsBiddingMoney());
                    }
                }
                //填充省内正在投标数和中标数量
                for (IsBiddingInfo biddingInfo : list1) {
                    if (biddingInfo.getDeptName().equals(isBiddingInfo.getDeptName())) {
                        isBiddingInfo.setIsBiddingNumberInProvince(biddingInfo.getIsBiddingNumberInProvince());
                        isBiddingInfo.setIsBiddingMoneyInProvince(biddingInfo.getIsBiddingMoneyInProvince());
                    }
                }
                //填充省外正在投标数和中标数量
                for (IsBiddingInfo biddingInfo : list2) {
                    if (biddingInfo.getDeptName().equals(isBiddingInfo.getDeptName())) {
                        isBiddingInfo.setIsBiddingNumberOutProvince(biddingInfo.getIsBiddingNumberOutProvince());
                        isBiddingInfo.setIsBiddingMoneyOutProvince(biddingInfo.getIsBiddingMoneyOutProvince());
                    }
                }
            }
        }
        //循环，对部门进行优化展示
        for (IsBiddingInfo isBiddingInfo : list3) {
            //封装数据
            if (isBiddingInfo.getIsBiddingNumber() == null) {
                isBiddingInfo.setIsBiddingNumber(0);
            }
            if (isBiddingInfo.getIsBiddingMoney() == null) {
                isBiddingInfo.setIsBiddingMoney(0L);
            }
            if (isBiddingInfo.getIsBiddingNumberInProvince() == null) {
                isBiddingInfo.setIsBiddingNumberInProvince(0);
            }
            if (isBiddingInfo.getIsBiddingMoneyInProvince() == null) {
                isBiddingInfo.setIsBiddingMoneyInProvince(0L);
            }
            if (isBiddingInfo.getIsBiddingNumberOutProvince() == null) {
                isBiddingInfo.setIsBiddingNumberOutProvince(0);
            }
            if (isBiddingInfo.getIsBiddingMoneyOutProvince() == null) {
                isBiddingInfo.setIsBiddingMoneyOutProvince(0L);
            }
        }
        List<IsBiddingInfo> collect = list3.stream().sorted(Comparator.comparing(IsBiddingInfo::getIsBiddingMoney).reversed()).collect(Collectors.toList());
        return collect;
    }



    /**
     * 三级菜单：获取各部门投标数详细信息
     */
    @Override
    public List<BidInfo> getDeptInfo(RequestData requestData, PageVo page) {
        int pageSize = page.getPageSize();
        int pageNumber = (page.getPageNumber() - 1) * pageSize;
        String time = requestData.getTime();
        String deptName1 = requestData.getDeptName();
        String deptName = deptName1.substring(0, 2);
        if (time.equals("本年")) {
            List<BidInfo> deptInfoByYear = bicMktBiddingInfoMapper.getDeptInfoByYear(deptName);
            List<BidInfo> deptInfoByYear1 = bicMktBiddingInfoMapper.getDeptInfoByYear1(deptName, pageNumber, pageSize);
            int size1 = deptInfoByYear.size();
            for (BidInfo deptInfo : deptInfoByYear1) {
//                判断是否中标
                String biddingId = deptInfo.getBiddingId();
               boolean isBidBack =  bicMktBiddingInfoMapper.isbidback(biddingId);
               if(isBidBack){
                   deptInfo.setIsBidBack("已中标");
               }
                deptInfo.setDeptName(deptName1);
//               对中标时间进行截取
                deptInfo.setBidBackTime(deptInfo.getBidBackTime().substring(0, 8));
                if (size1 != 0) {
                    Long total = (long) size1;
                    deptInfo.setTotal(total);
                } else {
                    deptInfo.setTotal(0L);
                }
            }
            return deptInfoByYear1;
        } else {
            List<BidInfo> deptInfoByYear = bicMktBiddingInfoMapper.getDeptInfoByMonth(deptName);
            List<BidInfo> deptInfoByYear1 = bicMktBiddingInfoMapper.getDeptInfoByMonth1(deptName, pageNumber, pageSize);
            int size1 = deptInfoByYear.size();
            for (BidInfo deptInfo : deptInfoByYear1) {
                deptInfo.setDeptName(deptName1);
                deptInfo.setBidBackTime(deptInfo.getBidBackTime().substring(0, 8));
//                判断是否中标
                String biddingId = deptInfo.getBiddingId();
                boolean isBidBack =  bicMktBiddingInfoMapper.isbidback(biddingId);
                if(isBidBack){
                    deptInfo.setIsBidBack("已中标");
                }
                if (size1 != 0) {
                    Long total = (long) size1;
                    deptInfo.setTotal(total);
                } else {
                    deptInfo.setTotal(0L);
                }
            }
            return deptInfoByYear1;
        }
    }

    /**
     * 三级菜单：获取各部门中标数详细信息
     */
    @Override
    public List<BidInfo> getDeptBidBackInfo(RequestData requestData, PageVo page) {
        int pageSize = page.getPageSize();
        int pageNumber = (page.getPageNumber() - 1) * pageSize;
        String time = requestData.getTime();
        String deptName1 = requestData.getDeptName();
        String deptName = deptName1.substring(0, 2);
        List<IsBiddingInfo> list = null;
        if (time.equals("本年")) {
            List<BidInfo> deptInfoByYear = bicMktBiddingInfoMapper.getDeptBidBackInfo(deptName);
            List<BidInfo> deptInfoByYear1 = bicMktBiddingInfoMapper.getDeptBidBackInfo1(deptName, pageNumber, pageSize);
            int size1 = deptInfoByYear.size();
            for (BidInfo deptInfo : deptInfoByYear1) {
                deptInfo.setDeptName(deptName1);
                deptInfo.setBidBackTime(deptInfo.getBidBackTime().substring(0, 8));
                if (size1 != 0) {
                    Long total = (long) size1;
                    deptInfo.setTotal(total);
                } else {
                    deptInfo.setTotal(0L);
                }
            }
            return deptInfoByYear1;
        } else {
            List<BidInfo> deptInfoByYear = bicMktBiddingInfoMapper.getDeptBidBackInfoByMonth(deptName);
            List<BidInfo> deptInfoByYear1 = bicMktBiddingInfoMapper.getDeptBidBackInfoByMonth1(deptName, pageNumber, pageSize);
            int size1 = deptInfoByYear.size();
            for (BidInfo deptInfo : deptInfoByYear1) {
                deptInfo.setDeptName(deptName1);
                deptInfo.setBidBackTime(deptInfo.getBidBackTime().substring(0, 8));
                if (size1 != 0) {
                    Long total = (long) size1;
                    deptInfo.setTotal(total);
                } else {
                    deptInfo.setTotal(0L);
                }
            }
            return deptInfoByYear1;
        }
    }

    /**
     * 三级菜单：获取各部门合同签订数详细信息
     */
    @Override
    public List<DeptInfo> getDeptContractInfo(RequestData requestData, PageVo page) {
        int pageSize = page.getPageSize();
        int pageNumber = (page.getPageNumber() - 1) * pageSize;
        String time = requestData.getTime();
        String deptName = requestData.getDeptName();
//        String deptName = "";
//        if (deptName1.equals("公司本部")){
//             deptName = "山东省信息产业服务有限公司";
//        }else {
//             deptName = deptName1.substring(0, 2);
//        }
        if (time.equals("本年")) {
            List<DeptInfo> deptInfoByYear = bicMktBiddingInfoMapper.getDeptContractInfo(deptName);
            List<DeptInfo> deptInfoByYear1 = bicMktBiddingInfoMapper.getDeptContractInfo1(deptName, pageNumber, pageSize);
            int size1 = deptInfoByYear.size();
            for (DeptInfo deptInfo : deptInfoByYear1) {
//                判断省内还是省外
                if (deptInfo.getContractArea().equals("35")) {
                    deptInfo.setContractArea("省内");
                } else {
                    deptInfo.setContractArea("省外");
                }
////               判断名称加上公司
//                deptInfo.setDeptName(deptName1);
                if (size1 != 0) {
                    Long total = (long) size1;
                    deptInfo.setTotal(total);
                } else {
                    deptInfo.setTotal(0L);
                }
            }
            return deptInfoByYear1;
        } else {
            List<DeptInfo> deptInfoByYear = bicMktBiddingInfoMapper.getDeptContractInfoByMonth(deptName);
            List<DeptInfo> deptInfoByYear1 = bicMktBiddingInfoMapper.getDeptContractInfoByMonth1(deptName, pageNumber, pageSize);
            int size1 = deptInfoByYear.size();
            for (DeptInfo deptInfo : deptInfoByYear1) {
//                判断省内还是省外
                if (deptInfo.getContractArea().equals("35")) {
                    deptInfo.setContractArea("省内");
                } else {
                    deptInfo.setContractArea("省外");
                }
////               判断名称加上公司
//                deptInfo.setDeptName(deptName1);
                if (size1 != 0) {
                    Long total = (long) size1;
                    deptInfo.setTotal(total);
                } else {
                    deptInfo.setTotal(0L);
                }
            }
            return deptInfoByYear1;
        }
    }

    /**
     * 三级菜单：获取各部门在投重大项目详细信息
     */
    @Override
    public List<BidInfo> getDeptIsBiddingInfo(RequestData requestData, PageVo page) {
        int pageSize = page.getPageSize();
        int pageNumber = (page.getPageNumber() - 1) * pageSize;
        String time = requestData.getTime();
        String deptName1 = requestData.getDeptName();
        String deptName = deptName1.substring(0, 2);
        if (time.equals("本年")) {
            List<BidInfo> deptInfoByYear = bicMktBiddingInfoMapper.getDeptIsBiddingInfo(deptName);
            List<BidInfo> deptInfoByYear1 = bicMktBiddingInfoMapper.getDeptIsBiddingInfo1(deptName, pageNumber, pageSize);
            int size1 = deptInfoByYear.size();
            for (BidInfo deptInfo : deptInfoByYear1) {
                deptInfo.setDeptName(deptName1);
                deptInfo.setBidBackTime(deptInfo.getBidBackTime().substring(0, 8));
                if (size1 != 0) {
                    Long total = (long) size1;
                    deptInfo.setTotal(total);
                } else {
                    deptInfo.setTotal(0L);
                }
            }
            return deptInfoByYear1;
        } else {
            List<BidInfo> deptInfoByYear = bicMktBiddingInfoMapper.getDeptIsBiddingInfoByMonth(deptName);
            List<BidInfo> deptInfoByYear1 = bicMktBiddingInfoMapper.getDeptIsBiddingInfoByMonth1(deptName, pageNumber, pageSize);
            int size1 = deptInfoByYear.size();
            for (BidInfo deptInfo : deptInfoByYear1) {
                deptInfo.setDeptName(deptName1);
                deptInfo.setBidBackTime(deptInfo.getBidBackTime().substring(0, 8));
                if (size1 != 0) {
                    Long total = (long) size1;
                    deptInfo.setTotal(total);
                } else {
                    deptInfo.setTotal(0L);
                }
            }
            return deptInfoByYear1;
        }
    }

    /**
     * 对未中标的进行删除处理
     */
    @Override
    public List<BicMktBiddingInfo> selectBidBack(List<BicMktBiddingInfo> records) {
        Iterator<BicMktBiddingInfo> iterator = records.iterator();
        while (iterator.hasNext()) {
            BicMktBiddingInfo record = iterator.next();
            String biddingId = record.getBiddingId();
            boolean result = bicMktBiddingInfoMapper.selectIsBidBack(biddingId);
            if (!result) {
                iterator.remove();
            }
        }
        return records;
    }

    @Override
    public List<String> getbiddingid() {
        List<String> list = bicMktBiddingInfoMapper.getbiddingid();
        return list;
    }

    @Override
    public List<BicMktBiddingInfo> getBidBackTime() {
        List<BicMktBiddingInfo> list = bicMktBiddingInfoMapper.getBidBackTime();
        return list;
    }
}
